const express = require('express');
const router = express.Router();
const Category = require('../../models/category');

// 获取分类列表
router.get('/list', async (req, res) => {
  try {
    const categories = await Category.find({ status: true })
      .sort({ sort: 1, createdAt: -1 });

    res.json({
      success: true,
      data: categories
    });
  } catch (error) {
    res.status(500).json({
      code:500,
      message: error.message
    });
  }
});

// 获取分类详情
router.get('/:id', async (req, res) => {
  try {
    const category = await Category.findById(req.params.id);

    if (!category) {
      return res.status(404).json({
        code:500,
        message: '分类不存在'
      });
    }

    res.json({
      success: true,
      data: category
    });
  } catch (error) {
    res.status(500).json({
      code:500,
      message: error.message
    });
  }
});

module.exports = router; 